package com.example.xyr.myapplication.base;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import com.example.xyr.myapplication.R;
import com.example.xyr.myapplication.utils.ActivityManger;
import com.example.xyr.myapplication.utils.MyIntent;
import com.example.xyr.myapplication.utils.StringUtil;
import com.jrmf360.tools.utils.SystemBarTintManager;
import com.sdsmdg.tastytoast.TastyToast;

import butterknife.ButterKnife;

/**
 * Created by ylcf on 2018/7/16.
 */

public abstract class BaseActivity extends FragmentActivity {

    public Activity mActivity;
    public  Bundle savedInstanceState;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ButterKnife.bind(this);
        mActivity = this;
        this.savedInstanceState = savedInstanceState;
        ActivityManger.Companion.getInstance().addActivity(this);
        tintStatusBar(0);
        init();
        initListener();
        initDate(MyIntent.Companion.getInstance());
    }

    public abstract int getLayout();

    public abstract void init();

    public abstract void initListener();

    public abstract void initDate(MyIntent intent);

    //成功的
    public void toastOk(String text) {
        if (StringUtil.isNotEmpty(text)) {
            TastyToast.makeText(this, text, TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
        }
    }

    //失败的
    public void toastNo(String text) {
        if (StringUtil.isNotEmpty(text)) {
            TastyToast.makeText(this, text, TastyToast.LENGTH_LONG, TastyToast.ERROR);
        }
    }

    //警告的
    public void toastWar(String text) {
        if (StringUtil.isNotEmpty(text)) {
            TastyToast.makeText(this, text, TastyToast.LENGTH_LONG, TastyToast.WARNING);
        }
    }

    public void onTouch(View view) {
        final long downTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(
                downTime, downTime, MotionEvent.ACTION_DOWN, 100, 100, 0);
        final MotionEvent upEvent = MotionEvent.obtain(
                downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 100, 100, 0);
        view.onTouchEvent(downEvent);
        view.onTouchEvent(upEvent);
        downEvent.recycle();
        upEvent.recycle();
    }

    protected void tintStatusBar(int colorInt) {
        String className = getClass().getSimpleName();
        int color = colorInt == 0 ? R.color.my_my_red : colorInt;
        if (className.equals("LoginActivity") || className.equals("MainActivity")) {
            return;
        }
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            int systemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
            systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
            systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
            getWindow().setStatusBarColor(getResources().getColor(color));
        } else if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintColor(getResources().getColor(color));
        }

    }
}
